<?php
/**
 * 文章数据模型
 * User: Lin Hui
 * Date: 2017/5/22
 * Time: 10:07
 */

namespace Common\Model;


use Think\Model;

class ArticleModel extends Model
{
    /**
     * 根据分类获取文章
     * @param int $category_id 分类ID
     * @param int $size 条数
     * @return array
     */
    public function getArticleByCategory($category_id, $size = 20)
    {
        if(!$category_id) {
            return array(null, null);
        }
        $type_category = array_search('category', C('ARTICLE.attribute'));
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        $total = $this->alias('a')
            ->where('a.status = 1')
            ->join($join)
            ->count();
        $page = new \Common\Lib\Page($total, $size);
        $list = $this->alias('a')
            ->join($join)
            ->where('a.status = 1')
            ->order('sort, create_time DESC')
            ->limit($page->firstRow, $size)
            ->field('a.article_id, a.title, a.thumb, a.create_time, a.click, a.content')
            ->select();
        foreach($list as $k => $item) {
        }
        return array($list, $page);
    }

    /**
     * 条件获取文章
     * @param $map
     * @param int $size
     * @return array
     */
    public function getArticleByMap($map, $size = 20)
    {
        $map = array_merge((array)$map, ['status'=>1]);
        $total = $this
            ->where($map)
            ->count();
        $page = new \Common\Lib\Page($total, $size);
        $list = $this
            ->where($map)
            ->order('sort, create_time DESC')
            ->limit($page->firstRow, $size)
            ->field('article_id, title, thumb, create_time, click, content')
            ->select();
        return array($list, $page);
    }

    /**
     * 获取文章信息
     * @param $article_id
     * @return bool|mixed
     */
    public function getInfo($article_id)
    {
        $article_id = intval($article_id);
        if($article_id) {
            return $this->find($article_id);
        } else{
            return false;
        }
    }

    /**
     * 获取上一篇（按发布时间为准）
     * @param $category_id
     * @param $time
     * @return mixed
     */
    public function getUpArticle($category_id, $time)
    {
        $type_category = array_search('category', C('ARTICLE.attribute'));
        if(!$category_id) {
            $article_id = I('id', 0, 'intval');
            $category_id = M('ArticleAttribute')->where(array('article_id'=>$article_id, 'type'=>array_search('category', C('ARTICLE.attribute'))))->getField('value');
            $category_id = $category_id ? $category_id : 0;
        }
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        return $this->alias('a')
            ->join($join)
            ->order('sort, create_time DESC')
            ->where("a.create_time < {$time} AND a.status = 1")
            ->field('a.article_id, a.title')
            ->find();
    }

    /**
     * 获取下一篇（按发布时间为准）
     * @param $category_id
     * @param $time
     * @return mixed
     */
    public function getDownArticle($category_id, $time)
    {
        $type_category = array_search('category', C('ARTICLE.attribute'));
        if(!$category_id) {
            $article_id = I('id', 0, 'intval');
            $category_id = M('ArticleAttribute')->where(array('article_id'=>$article_id, 'type'=>array_search('category', C('ARTICLE.attribute'))))->getField('value');
            $category_id = $category_id ? $category_id : 0;
        }
        $join = "
                INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id
                ";
        return $this->alias('a')
            ->join($join)
            ->order('sort, create_time')
            ->where("a.create_time > {$time} AND a.status = 1")
            ->field('a.article_id, a.title')
            ->find();
    }

    /**
     * 添加点击
     * @param $id
     * @return bool
     */
    public function addClick($id)
    {
        return $this->where(array('article_id'=>$id))
            ->save(array(
                'click'=>array('exp', 'click + 1')
            ));
    }

    /**
     * 获取推荐文章
     * @param $size
     * @param int $category_id
     */
    public function getRecommend($size, $category_id = 0)
    {
        $type_category = array_search('category', C('ARTICLE.attribute'));
        if($category_id){
            $join = "INNER JOIN __ARTICLE_ATTRIBUTES__ category ON category.value = {$category_id} AND category.type = {$type_category} AND a.article_id = category.article_id";
        }
        $list = $this->alias('a')
            ->join($join)
            ->where(array('recommend'=>1, 'status'=>1))
            ->order('sort, create_time DESC')
            ->limit($size)
            ->field('a.article_id, a.title, a.thumb, a.create_time, a.click, a.content')
            ->select();
        foreach($list as $k => $item) {
        }
        return $list;
    }

    /**
     * 获取置顶文章
     * @return mixed
     */
    public function getTop()
    {
        $list = $this
            ->where(array('top'=>1, 'status'=>1))
            ->field('article_id, title, thumb, create_time, click, content')
            ->find();
        foreach($list as $k => $item) {
        }
        return $list;
    }

    /**
     * 获取文章分类
     * @param $article_id
     * @return mixed
     */
    public function getCategory($article_id)
    {
        $attr = new ArticleAttributesModel();
        $cat_id = $attr->where(array('article_id'=>$article_id, 'type'=>array_search('category', C('ARTICLE.attribute'))))->getField('value');
        return array_search($cat_id, C('ARTICLE.category'));
    }
}